import { getAreaConfig } from '@/utils/filterConfig';
import { getBondType, getCaliberType, getVarietyType, getGreenCertificationResult } from './../../../apis';
import { ComponentType, type FilterMenuProps } from 'vc';

export function useScreen(companyId) {
  const PAGE_SIZE = 15;

  const queryData = reactive<any>({
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    sort: 'desc'
  });

  // 地区
  const areaData = getAreaConfig();
  areaData.value.submitFn = (val) => {
    const { lv1, lv2, lv3 } = val;

    queryData.provinceCode = lv1.join();
    queryData.cityCode = lv2.join();
    queryData.countryCode = lv3.join();
  };

  // 认证结果
  const { data: certificateResultData } = useRequest(getGreenCertificationResult, {
    defaultParams: [{ id: companyId }]
  });
  const certificateResultTypeData = {
    title: '认证结果',
    component: ComponentType.MULTIPLE,
    options: certificateResultData,
    required: false,
    submitFn: (data) => {
      queryData.certificationResult = data;
    }
  };

  // 绿债口径分类
  const { data: caliberData } = useRequest(getCaliberType, {});

  // 绿债品种分类
  const { data: varietyData } = useRequest(getVarietyType, {});

  // 债券类型
  const { data } = useRequest(getBondType, {});

  // 发行日期
  const dateData = {
    title: '发行日期',
    teleported: false,
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.issuingDateStart = data[0];
      queryData.issuingDateEnd = data[1];
    }
  };
  // 认证日期
  const dateCertificateData = {
    title: '认证日期',
    teleported: false,
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.certificationDateStart = data[0];
      queryData.certificationDateEnd = data[1];
    }
  };

  const bondTypeDataMoreOption = computed(() => {
    return [
      {
        name: '绿债口径分类',
        code: 'greenBondCaliber',
        data: caliberData,
        isNoLimit: true
      },
      {
        name: '绿债品种分类',
        code: 'greenBondVariety',
        data: varietyData,
        isNoLimit: true
      },
      {
        name: '债券类型',
        code: 'bondType',
        data: data,
        multiple: true,
        isNoLimit: true
      }
    ];
  });
  // 更多
  const moreBondTypeFilter: FilterMenuProps = {
    title: '债券分类',
    component: ComponentType.TILED,
    options: bondTypeDataMoreOption,
    submitFn: (data) => {
      Object.assign(queryData, data);
    }
  };

  // 是否到期
  const maturityTypeData = {
    title: '是否到期',
    component: ComponentType.SINGLE,
    options: [
      { name: '是', code: 2 },
      { name: '否', code: 1 }
    ],
    required: false,
    submitFn: (data) => {
      queryData.maturityType = data;
    }
  };

  const screen = ref<FilterMenuProps[]>([moreBondTypeFilter, dateData, dateCertificateData, certificateResultTypeData, maturityTypeData]);

  return {
    PAGE_SIZE,
    screen,
    queryData
  };
}
